package parking.abcast;

import java.io.Serializable;

import parking.Message;

/**
 * Class used to represent a message when diffusing to the group
 * @author Sophie and Ronnie
 * @date 9 November 2013
 */
public class MessageDiffusion implements Serializable, Comparable<MessageDiffusion> {
	
	private static final long serialVersionUID = -537418419282574724L;
	private int typeMessage;		//0 for an entry, 1 for an exit
	private float estIdEm;			//clock.idSender
	private Message ms;				//message status, PENDING or READY
	private int emetteurOrigine;	//ID of the original sender
	private String idMessage;		//corresponds to the first value clock.idSender
	
	/**
	 * Constructor for the class MessageDiffusion
	 * @param typeMessage : indicates whether it is an entry or an exit
	 * @param estampilleProcessus : the current internal clock of the process when diffusing the message
	 * @param idEmetteur : id of the process diffusing a message
	 */
	public MessageDiffusion(int typeMessage, int estampilleProcessus, int idEmetteur) {
		this.emetteurOrigine = idEmetteur;
		//is unique in the system
		this.idMessage = new String(estampilleProcessus + "." + idEmetteur);
		Float newEstIdEm = new Float(estampilleProcessus + "." + idEmetteur);
		this.estIdEm = newEstIdEm.floatValue();
		this.typeMessage = typeMessage;
		this.ms = Message.PENDING;
	}
	
	/**
	 * Get the type of message
	 * @return an entry or an exit
	 */
	public int getTypeMessage() {
		return this.typeMessage;
	}

	/**
	 * Get the current message status
	 * @return PENDING or READY
	 */
	public Message getMessageStatus() {
		return ms;
	}

	/**
	 * Allows the modification of the message status
	 * @param ms : The new status
	 */
	public void setMessageStatus(Message ms) {
		this.ms = ms;
	}
	
	/**
	 * Get the current clock.idSender value
	 * @return a float representing this value
	 */
	public float getEstIdEm() {
		return this.estIdEm;
	}
	
	/**
	 * Allows the modification of the current value clock.idSender
	 * @param newEstIdEm : the new clock.idSender
	 */
	public void setEstIdEm(float newEstIdEm) {
		this.estIdEm = newEstIdEm;
	}

	/**
	 * Get the ID of the original sender
	 * @return an integer representing this value
	 */
	public int getEmetteurOrigine() {
		return this.emetteurOrigine;
	}
	
	/**
	 * Get the ID of the message. The ID is unique in the system
	 * @return a string representing the ID of the message
	 */
	public String getIdMessage() {
		return this.idMessage;
	}

	/**
	 * Overridden method of the Comparable interface
	 * This method is used when sorting the list of all received messages
	 */
	@Override
	public int compareTo(MessageDiffusion o) {
		if (this.estIdEm > o.estIdEm) return 1;
		return -1;
	}
	
	/**
	 * toString method which allows to represent the information used in this class
	 * @return a string representing these information
	 */
	public String toString() {
		String retour = "";
		retour = this.idMessage + " | " + this.typeMessage + " | " + this.estIdEm + " | " + this.ms;
		return retour;
	}

}
